package ctf.evaluation.SimulatorTests;

import java.io.IOException;

import ctf.evaluation.TestCase;
import ctf.evaluation.simulator.ConnectionManager;
import ctf.evaluation.simulator.ConnectionManager.STEALState;
import ctf.evaluation.simulator.responses.CommandResponse;
import ctf.evaluation.simulator.responses.GameOverException;
import ctf.evaluation.simulator.responses.RoundOverException;
import ctf.evaluation.simulator.responses.StateResponse;
import ctf.model.Side;
import ctf.network.NetworkException;
import ctf.network.ProtocolError;
import ctf.network.SocketClosedException;
import ctf.network.UninitializedSocketException;
import ctf.network.tcp.Connection;
import ctf.evaluation.simulator.data.Player;
import ctf.evaluation.simulator.data.State;

public class PlayerTests extends TestCase {

	private ConnectionManager connections = ConnectionManager.instance();
	private Connection red, blue;
	
	@Override
	public void setUp() throws Exception {
		connections.enterState(STEALState.PLAY);
		red = connections.red();
		blue = connections.blue();
	}
	
	@Override public void tearDown() {};
	
	/**
	 * Tests the player max forward speed of 5 m/s
	 * @throws IOException
	 * @throws NetworkException
	 * @throws ProtocolError
	 * @throws RoundOverException
	 * @throws GameOverException
	 */
	public void testForwardMaximumSpeed() throws IOException, NetworkException, ProtocolError, RoundOverException, GameOverException{
		red.sendLine("accelerate 0 0 2.0");
		CommandResponse response = new CommandResponse(red);
		//sleep for 3 seconds to allow speed to possibly exceed 5
		sleep(3000);
		
		red.sendLine("getState");
		State state = (new StateResponse(red)).state();
		Player player = state.team(Side.LEFT).player(0);
		assertClose("Max speed not more than 5.0", 5.0, player.speed());
		
	}
	/**
	 * Test to see if angular acceleration works after a delay
	 * @throws IOException
	 * @throws NetworkException
	 * @throws ProtocolError
	 * @throws RoundOverException
	 * @throws GameOverException
	 */
	public void testDelayedAngularVelocity() throws IOException, NetworkException, ProtocolError, RoundOverException, GameOverException{
		red.sendLine("getState");
		State state = (new StateResponse(red)).state();
		Player playerInitial = state.team(Side.LEFT).player(0);
		
		red.sendLine("accelerate 0 0 2.0");
		CommandResponse response = new CommandResponse(red);

		red.sendLine("spin 0 1 20");
		response = new CommandResponse(red);

		
		sleep(2000);
		
		red.sendLine("getState");
		state = (new StateResponse(red)).state();
		Player playerFinal = state.team(Side.LEFT).player(0);
		
		if(playerInitial.location().x == playerFinal.location().x){
			fail();
		}
		
		if(playerInitial.location().y == playerFinal.location().y){
			fail();
		}
			
		assertTrue(true);
		
	}
	
}
